package com.tj.jinzao.activity;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.tj.jinzao.R;
import com.tj.jinzao.base.BaseActivity;
import com.tj.jinzao.base.ConstantApp;
import com.tj.jinzao.callback.IUploadView;
import com.tj.jinzao.model.Identification;
import com.tj.jinzao.model.Target;
import com.tj.jinzao.presenter.IdentifyPresenter;
import com.tj.jinzao.utils.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindViews;
import butterknife.OnClick;

import static com.luck.picture.lib.config.PictureConfig.CHOOSE_REQUEST;

/**
 * 我的身份认证界面
 * Created by wjy on 2019/1/5.
 */
public class MyIdentificationOccupation1Activity extends BaseActivity<IdentifyPresenter> implements IUploadView<Identification> {

    @BindViews({R.id.iv_card_1, R.id.iv_card_2, R.id.iv_card_3, R.id.iv_card_4, R.id.iv_card_5})
    List<ImageView> mCardViews;

    private List<String> list=new ArrayList<>();

    private boolean mIsReal;

    @Override
    protected IdentifyPresenter createPresenter() {
        return new IdentifyPresenter(this);
    }

    @Override
    public int getLayoutResource() {
        return R.layout.activity_my_identification_occupation;
    }
    @Override
    protected void setupView() {
        super.setupView();
        mPresenter.isRealName();
    }

    @Override
    protected int getLeftButtonResId() {
        return R.mipmap.ic_back;
    }

    @Override
    protected CharSequence getRightButtonText() {
        return "确认";
    }

    @Override
    protected void clickRightButton() {
        if(ConstantApp.isFastDoubleClick(1500)){
            return;
        }
        if(list.size()<=0){
            ToastUtils.showToast(MyIdentificationOccupation1Activity.this,"至少上传一张图片");
            return;
        }
        uploadFile();
    }

    //身份证认证
    @OnClick(R.id.iv_card_5)
    public void identification() {
        if (mIsReal) {
            if(list.size()>=5){
                ToastUtils.showToast(MyIdentificationOccupation1Activity.this,"最多选择5张照片");
                return;
            }
            PictureSelector.create(this)
                    .openGallery(PictureMimeType.ofImage())
                    .maxSelectNum(1)
                    .compress(true)
                    .forResult(PictureConfig.CHOOSE_REQUEST);
        }else {
            ToastUtils.showToast(MyIdentificationOccupation1Activity.this,"尚未实名认证，请先进行实名认证");
            Intent to=new Intent();
            to.setClass(MyIdentificationOccupation1Activity.this,MyIdentificationRealActivity.class);
            startActivity(to);
        }
    }
    private void uploadFile() {
        imgSum=0;
        for (int i=0;i<list.size();i++) {
            mPresenter.uploadFile(list.get(i));
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case CHOOSE_REQUEST:
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    for (LocalMedia localMedia : selectList) {
                                list.add(localMedia.isCompressed() ? localMedia.getCompressPath() : localMedia.getPath());
                                mCardViews.get(list.size()-1).setVisibility(View.VISIBLE);
                                Glide.with(this).load(list.get(list.size()-1)).into(mCardViews.get(list.size()-1));
                                break;
                    }
                    break;
            }
        }
    }
    private int imgSum;
    private String mImageUrl;

    @Override
    public void onUploadSuccess(String path, String url) {
        imgSum++;
        mImageUrl="";
        for (int i=0;i<list.size();i++) {
            if (TextUtils.equals(list.get(i), path)) {
                list.set(i,url);
                break;
            }
        }

        for (int i=0;i<list.size();i++){
            if(i==0){
                mImageUrl=mImageUrl+list.get(0);
            }else {
                mImageUrl=mImageUrl+","+list.get(i);
            }
        }
        if(imgSum==list.size()){
            finish();
            startActivity(new Intent(this, MyIdentificationOccupation2Activity.class).putExtra("image",mImageUrl));
        }

    }

    @Override
    public void onCaseSuccess(List<Target> list) {

    }

    @Override
    public void onCaseFailed(String msg) {

    }

    @Override
    public Activity getContext() {
        return this;
    }

    @Override
    public void onRequestSuccess(Identification identification) {
        mIsReal = (identification != null && identification.getId() != 0);
    }

    @Override
    public void onRequestNoData(String message) {

    }

    @Override
    public void onRequestError(String error_message) {

    }
}
